package org.leumasjaffe.initiativeTracker.event;


import java.awt.event.ActionEvent;

import javax.swing.Action;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.entity.Character;
import org.leumasjaffe.initiativeTracker.entity.PC;
import org.leumasjaffe.initiativeTracker.operator.CharacterLinker;
import org.leumasjaffe.initiativeTracker.view.InitiativeFrame;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.ServerAccess;

@SuppressWarnings("serial")
public class HealingSurgeAction extends InitiativeTrackerAction implements Action {

	public HealingSurgeAction (String text) {
		super(text);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String xmlString = Message.requestHeader() + "<surgeRequest game='" + Constants.id + "'>";
		
		int pageNum = Constants.pageNum;
		int pageLength = InitiativeFrame.pageLength;
		for (int i = 0 ; i < list.size(); i++) {
			CharacterLinker cc = list.get(i);
			Character c = cc.getCharacter();
			if (c instanceof PC && cc.isTarget()) {
				float heal = CharacterLinker.heal((PC) c);
				if (heal != -0.5F)
					xmlString += "<target id='" + c.getId() + "' value='" + ((int) heal) + "'/>";
				cc.update(i - (pageNum * pageLength));
			}
		}
		
		xmlString += "</surgeRequest></request>";
		if (xmlString.contains("target"))
			ServerAccess.getInstance().sendRequest(new Message(xmlString));
		
		gui.repaint();
	}
	
}
